package com.multiplay;

import java.io.IOException;

import com.multiplay.tetris.MainWindow;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class NetconnActivity extends Activity 
{
    private Thread com_thread = null;
    private ServerMainLoop main_loop = null;
    private Discoverer cli_loop = null;
    private CheckBox is_server;
    private Button start_stop;
    private TextView info_view = null;
    
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        is_server = (CheckBox)findViewById(R.id.start_as_server);
        start_stop = (Button)findViewById(R.id.start_stop);
        info_view = (TextView)findViewById(R.id.info_view);

        /* Don't turn off the screen when application is running */
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    protected void stop_thread(BaseNetLoop th)
    {
        if (th == null)
            return;

        th.stop_loop();
        try 
        {
            com_thread.join();
        }
        catch (InterruptedException e) 
        {
            Log.e(GlobalDefs.LOG_APP_TAG, e.getMessage());
        }

        info_view.setText("");
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.v(GlobalDefs.LOG_APP_TAG, "onDestroy entered");

        stop_thread(main_loop);
        stop_thread(cli_loop);

        Log.v(GlobalDefs.LOG_APP_TAG, "onDestroy leaved");
    }

    private boolean start_server()
    {
        boolean rc = true;

        try 
        {
            main_loop = new ServerMainLoop(this);
            WifiManager wifi =
                (WifiManager)getSystemService(Context.WIFI_SERVICE);
            main_loop.init(wifi);
            com_thread = new Thread(main_loop);

            com_thread.start();
            info_view.setText("Server: Waiting for connect");
        }
        catch (IOException e)
        {
            com_thread = null;
            Log.e(GlobalDefs.LOG_APP_TAG, e.getMessage());
            rc = false;

        }

        return rc;
    }
    private boolean start_client()
    {
        boolean rc = true;

        try 
        {
            cli_loop = new Discoverer(this);
            WifiManager wifi =
                (WifiManager)getSystemService(Context.WIFI_SERVICE);
            cli_loop.init(wifi);
            com_thread = new Thread(cli_loop);

            com_thread.start();
            info_view.setText("Client: Looking for connect");
        }
        catch (IOException e) 
        {
            Log.e(GlobalDefs.LOG_APP_TAG, e.getMessage());
            com_thread = null;
            rc = false;
        }
        return rc;
    }
    
    protected void set_running_state(boolean enable)
    {
        is_server.setClickable(enable);
        is_server.setEnabled(enable);
        start_stop.setText(enable ? "Start" : "Stop");
    }
    
    public void start_single_game(View view)
    {
        Log.v(GlobalDefs.LOG_APP_TAG, "Start play activity");

        Intent play_view = new Intent(this, MainWindow.class);
        startActivity(play_view);
    }
    
    public void start_net_conn(View view)
    {
        if (is_server.isChecked())
        {
            if (com_thread == null)
            {
                start_server();
            }
            else
            {
                stop_thread(main_loop);
                main_loop = null;
                com_thread = null;
            }
        }
        else
        {
            if (com_thread == null)
            {
                start_client();
            }
            else
            {
                stop_thread(cli_loop);
                cli_loop = null;
                com_thread = null;
            }
        }
        set_running_state((com_thread == null) ? true : false);
    }
}
